package com.whisper.view;

import com.whisper.barrage.BarrageEntity;
import com.whisper.utils.MyListener;
import com.whisper.utils.ZTextField;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class GlassPaneInput extends JTextField {

    MyListener myListener = new MyListener();

    ZTextField jTextField = null;

    public void paintInputPane(JFrame jFrame) {
        paintSettingButton(jFrame);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowHeight = (int) screenSize.getHeight();
        int windowWidth = (int) screenSize.getWidth();

        jTextField = new ZTextField(1,20);
        // 位置
        jTextField.setBounds(windowWidth - 250,windowHeight - 300,250,50);
//        jTextField.setPreferredSize (new Dimension (280, 20));
        // 边框
        jTextField.setBorder(new LineBorder(new Color(214, 215, 220),10 ,true));
        // 背景色
        jTextField.setBackground (new Color(214, 215, 220));

        // 透明度
//        jTextField.setOpaque(false);
        // 选中颜色
        jTextField.setSelectionColor(Color.lightGray);
        jTextField.setForeground(Color.BLACK);
        jTextField.setFont(new Font("微软雅黑",TextField.ALLBITS, 15));

        jTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER && 0 != jTextField.getText().length())   //按回车键执行相应操作;
                {
                    BarrageEntity content = new BarrageEntity(BarrageEntity.WHITE, 12, BarrageEntity.TextSmart, jTextField.getText());
                    WindowBarrage.getInstance().addBarrage(content);
                    jTextField.setText("");
                }
            }
        });

//        jTextField.addMouseListener(myListener);
        jFrame.add(jTextField);
    }

    public void paintSettingButton(JFrame jFrame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowHeight = (int) screenSize.getHeight();
        int windowWidth = (int) screenSize.getWidth();

        URL url = getClass().getResource("/img/setting.png");
        //创建ImageIcon
        ImageIcon icon = new ImageIcon(url);
        //把图标设置给JButton/JLabel
        JLabel iconLabel = new JLabel();
        icon.setImage(icon.getImage().getScaledInstance(50, 30,
                Image.SCALE_DEFAULT));
        iconLabel.setIcon(icon);
        iconLabel.setBounds(windowWidth - 60,windowHeight - 295,60,40);

//        iconLabel.addMouseListener(myListener);
        iconLabel.addMouseMotionListener(new MouseAdapter() {
            //这两组x和y为鼠标点下时在屏幕的位置和拖动时所在的位置
            int newX,newY,oldX,oldY;
            //这两个坐标为组件当前的坐标
            int startX,startY;

            @Override
            public void mousePressed(MouseEvent e) {
                //此为得到事件源组件
                Component cp = (Component)e.getSource();
                //当鼠标点下的时候记录组件当前的坐标与鼠标当前在屏幕的位置
                startX = cp.getX();
                startY = cp.getY();
                oldX = e.getXOnScreen();
                oldY = e.getYOnScreen();
            }
            @Override
            public void mouseDragged(MouseEvent e) {
                Component cp = (Component)e.getSource();
                //拖动的时候记录新坐标
                newX = e.getXOnScreen();
                newY = e.getYOnScreen();
                //设置bounds,将点下时记录的组件开始坐标与鼠标拖动的距离相加
                cp.setBounds(startX+(newX - oldX), startY+(newY - oldY), cp.getWidth(), cp.getHeight());
                jTextField.setBounds(startX + (newX - oldX) - 190, startY + (newY - oldY) - 5,250,50);
            }
        });

        iconLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                paintSettingDetails();
            }
        });
        jFrame.add(iconLabel);
    }

    private void paintSettingDetails() {

    }
}